package main;

import db.MYSQLControl;
import java.util.ArrayList;
import java.util.List;
import model.UrlModel;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class ListLinks {

  public static void main(String[] args) throws Exception {
    String url = "http://www.zouchengtc.cn/";
    print("Fetching %s...", url);

    Document doc = Jsoup.connect(url).get();
    Elements links = doc.select("div[class=listblock]")
        .select("div[class=item]").select("ul").select("li");
//获取的数据，存放在集合中
    List<UrlModel> data = new ArrayList<UrlModel>();
    print("\nLinks: (%d)", links.size());
    for (Element link : links) {
      String url2 = link.select("span[class=contenttitle]").select("a[href]")
          .attr("abs:href");
      print(" a: %s", url2);
      UrlModel urlModel = new UrlModel();
      //对象的值
      urlModel.setUrl(url2);
      //将每一个对象的值，保存到List集合中
      data.add(urlModel);

    }
    //将抓取的数据插入数据库
//      MYSQLControl.executeInsert2(data);
  }

  private static void print(String msg, Object... args) {
    System.out.println(String.format(msg, args));
  }

  private static String trim(String s, int width) {
    if (s.length() > width) {
      return s.substring(0, width - 1) + ".";
    } else {
      return s;
    }
  }
}
